
-- $Id: Gui.lua 72 2009-04-06 11:38:08Z Fylgjas $

-- ========================================================================= --
-- Copyright © 2009 Thomas ROBERT (aka Fylgjas EU - Marécage de Zangar)      --
-- ========================================================================= --
-- This file is part of FyLooter.                                            --
--                                                                           --
-- FyLooter is free software: you can redistribute it and/or modify it under --
-- the terms of the GNU General Public License as published by the Free      --
-- Software Foundation, either version 3 of the License, or (at your option) --
-- any later version.                                                        --
--                                                                           --
-- FyLooter is distributed in the hope that it will be useful, but WITHOUT   --
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     --
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for  --
-- more details.                                                             --
--                                                                           --
-- You should have received a copy of the GNU General Public License along   --
-- with FyLooter. If not, see <http://www.gnu.org/licenses/>.                --
-- ========================================================================= --

local revision = tonumber(string.sub("$Rev: 72 $", 7, -3));

if (FyLooter.revision < revision) then
  FyLooter.revision = revision;
end

-- ========================================================================= --

-- 
-- _close()
--
local function _close(self)
  FyLooter:Close();
end

-- 
-- _apply()
--
local function _apply(self)
  FyLooter:Delete(FyLooter.players_bkp);

  FyLooter:Commit();

  FyLooter:Close();
end

-- 
-- _cancel()
--
local function _cancel(self)
  if (FyLooter.pending) then
    FyLooter.db.players = FyLooter.players_bkp;
    FyLooter.ScrollFrame.model:Update();
  end

  FyLooter:Delete(FyLooter.players_bkp);

  FyLooter:Close();
end

-- 
-- _bid()
--
local function _bid(self)
  FyLooter:AnswerBid(self:GetID());

  FyLooter.BidFrame:Hide();
  FyLooter.ScrollFrame:Show();

  if (FyLooter.loot.ml == UnitName("player")) then
    FyLooter.MasterFrame:Show();
  end
end

-- 
-- _apply_insert()
--
local function _apply_insert(self)
  FyLooter.ScrollFrame.model:Insert(FyLooter.InsertFrame.PlayerEditBox:GetText(), FyLooter.InsertFrame.CharacterEditBox:GetText());
  FyLooter.InsertFrame.CharacterEditBox:SetText("");

  FyLooter.ControlFrame.OkButton:SetScript("OnClick", _apply);
  FyLooter.ControlFrame.OkButton:SetText("Apply");
  FyLooter.ControlFrame.CancelButton:SetScript("OnClick", _cancel);
  
  FyLooter.InsertFrame:Hide();
  FyLooter.AdminFrame:Show();
end

-- 
-- _cancel_insert()
--
local function _cancel_insert(self)
  FyLooter.ControlFrame.OkButton:SetScript("OnClick", _apply);
  FyLooter.ControlFrame.OkButton:SetText("Apply");
  FyLooter.ControlFrame.CancelButton:SetScript("OnClick", _cancel);

  FyLooter.InsertFrame:Hide();
  FyLooter.AdminFrame:Show();
end

-- 
-- _insert()
--
local function _insert(self)
  FyLooter.ControlFrame.OkButton:SetScript("OnClick", _apply_insert);
  FyLooter.ControlFrame.OkButton:SetText("Insert");
  FyLooter.ControlFrame.CancelButton:SetScript("OnClick", _cancel_insert);
  
  FyLooter.AdminFrame:Hide();
  FyLooter.InsertFrame:Show();
end

-- 
-- _remove()
--
local function _remove(self)
  self:Disable();

  local model = FyLooter.ScrollFrame.model;
  local idx = model:GetSelection();

  model:Remove(idx);

  if (idx <= model:GetNumItems()) then
    model:SetSelection(idx);
  else
    if (idx > 1) then
      model:SetSelection(idx - 1);
    else
      model:SetSelection(nil);
    end
  end
end

-- 
-- _move_up()
--
local function _move_up(self)
  FyLooter.AdminFrame.UpButton:Disable();
  FyLooter.AdminFrame.DownButton:Disable();

  local model = FyLooter.ScrollFrame.model;
  local idx = model:GetSelection();
  model:Swap(idx, idx - 1);
  model:SetSelection(idx - 1);

  FyLooter.ScrollFrame:Update();
end

-- 
-- _move_down()
--
local function _move_down(self)
  FyLooter.AdminFrame.UpButton:Disable();
  FyLooter.AdminFrame.DownButton:Disable();

  local model = FyLooter.ScrollFrame.model;
  local idx = model:GetSelection();
  model:Swap(idx, idx + 1);
  model:SetSelection(idx + 1);

  FyLooter.ScrollFrame:Update();
end

-- 
-- _loot()
--
local function _loot(self)
  local action = self:GetID();

  if (action < 3) then
    local idx = FyLooter.ScrollFrame.model:GetSelection();
    local player = FyLooter.ScrollFrame.model.index[idx]
    GiveMasterLoot(FyLooter.loot.slot, player);

    if (action == 1) then
      FyLooter.ScrollFrame.model:Kill(idx);

      SendChatMessage(FyLooter.loot.item .. " attribué à " .. player .. " en pay loot.", "RAID_WARNING");
    else
      SendChatMessage(FyLooter.loot.item .. " attribué à " .. player .. " en free loot.", "RAID_WARNING");
    end
  end

  local sdata = FyLooter:Serialize("LOOT_CLEARED", {
    ["loot"] = FyLooter.loot,
  });

  FyLooter:SendCommMessage("FyLooter_Comm", sdata, "RAID");
end

-- ========================================================================= --

-- 
-- FyLooter:SetupFrames()
--
function FyLooter:SetupFrames()
  -- FyLooter.Anchor
  self.Anchor = CreateFrame("Frame", "FyLooter_Anchor", UIParent);
  self.Anchor:Hide();
  self.Anchor:SetWidth(254);
  self.Anchor:SetPoint("CENTER");
  self.Anchor:SetToplevel(true);
  self.Anchor:SetMovable(true);
  self.Anchor:SetClampedToScreen(true);
  self.Anchor:EnableMouse(true);
  self.Anchor:SetScript("OnHide", Anchor_OnHide);
  
  -- FyLooter.TitleFrame
  self.TitleFrame = self:CreateTitleFrame(self.Anchor);
  self.TitleFrame:SetTitle(format("FyLooter %s", GetAddOnMetadata("FyLooter", "Version")));
  self.TitleFrame:SetPoint("TOPLEFT");
  self.TitleFrame:SetPoint("TOPRIGHT");
  
  -- FyLooter.ScrollFrame
  self.ScrollFrame = self:CreateScrollFrame(self.Anchor);
  self.ScrollFrame:SetPoint("TOPLEFT", self.TitleFrame, "BOTTOMLEFT");
  self.ScrollFrame:SetPoint("TOPRIGHT", self.TitleFrame, "BOTTOMRIGHT");

  -- FyLooter.LootFrame
  self.LootFrame = self:CreateLootFrame(self.Anchor);
  self.LootFrame:SetPoint("TOPLEFT", self.TitleFrame, "BOTTOMLEFT");
  self.LootFrame:SetPoint("TOPRIGHT", self.TitleFrame, "BOTTOMRIGHT");
  
  -- FyLooter.MasterFrame
  self.MasterFrame = self:CreateMasterFrame(self.Anchor);
  self.MasterFrame:SetPoint("TOPLEFT", self.LootFrame, "BOTTOMLEFT");
  self.MasterFrame:SetPoint("TOPRIGHT", self.LootFrame, "BOTTOMRIGHT");
  self.MasterFrame.PayButton:SetScript("OnClick", _loot);
  self.MasterFrame.FreeButton:SetScript("OnClick", _loot);
  self.MasterFrame.CancelButton:SetScript("OnClick", _loot);

  -- FyLooter.BidFrame
  self.BidFrame = self:CreateBidFrame(self.Anchor);
  self.BidFrame:SetPoint("TOPLEFT", self.LootFrame, "BOTTOMLEFT");
  self.BidFrame:SetPoint("TOPRIGHT", self.LootFrame, "BOTTOMRIGHT");
  self.BidFrame.NeedButton:SetScript("OnClick", _bid);
  self.BidFrame.GreedButton:SetScript("OnClick", _bid);
  self.BidFrame.PassButton:SetScript("OnClick", _bid);

  -- FyLooter.AdminFrame
  self.AdminFrame = self:CreateAdminFrame(self.Anchor);
  self.AdminFrame:SetPoint("TOPLEFT", self.ScrollFrame, "BOTTOMLEFT");
  self.AdminFrame:SetPoint("TOPRIGHT", self.ScrollFrame, "BOTTOMRIGHT");
  self.AdminFrame.InsertButton:SetScript("OnClick", _insert);
  self.AdminFrame.RemoveButton:SetScript("OnClick", _remove);
  self.AdminFrame.UpButton:SetScript("OnClick", _move_up);
  self.AdminFrame.DownButton:SetScript("OnClick", _move_down);

  -- FyLooter.InsertFrame
  self.InsertFrame = self:CreateInsertFrame(self.Anchor);
  self.InsertFrame:SetPoint("TOPLEFT", self.ScrollFrame, "BOTTOMLEFT");
  self.InsertFrame:SetPoint("TOPRIGHT", self.ScrollFrame, "BOTTOMRIGHT");

  -- FyLooter.ControlFrame
  self.ControlFrame = self:CreateControlFrame(self.Anchor);
  self.ControlFrame:SetPoint("BOTTOMLEFT");
  self.ControlFrame:SetPoint("BOTTOMRIGHT");
end

-- ========================================================================= --

-- 
-- FyLooter:Open()
--
function FyLooter:Open(admin)
  if (self.Anchor:IsVisible()) then return end

  self.ScrollFrame:SetModel(self.CharactersModel);
  
  local height = self.TitleFrame:GetHeight() + self.ScrollFrame:GetHeight() + self.ControlFrame:GetHeight();

  self.TitleFrame:Show();
  self.ScrollFrame:Show();

  if (admin and FyLooter.ADMIN) then
    self.players_bkp = self:Clone(self.players);

    height = height + self.AdminFrame:GetHeight();
    self.AdminFrame:Show();

    self.ControlFrame.OkButton:SetScript("OnClick", _apply);
    self.ControlFrame.OkButton:SetText("Apply");
    self.ControlFrame.CancelButton:SetScript("OnClick", _cancel);
    self.ControlFrame.CancelButton:Show();
  else
    self.ControlFrame.OkButton:SetScript("OnClick", _close);
    self.ControlFrame.OkButton:SetText("Close");
    self.ControlFrame.CancelButton:Hide();
  end

  self.ControlFrame:Show();

  self.Anchor:SetHeight(height);
  self.Anchor:Show();
end

-- 
-- FyLooter:Close()
--
function FyLooter:Close()
  self.Anchor:Hide();
  self.Anchor:SetHeight(0);

  self.TitleFrame:Hide();
  self.ScrollFrame:Hide();
  self.LootFrame:Hide();
  self.BidFrame:Hide();
  self.MasterFrame:Hide();
  self.AdminFrame:Hide();
  self.InsertFrame:Hide();
  self.ControlFrame:Hide();
end

-- ========================================================================= --

